package com.fieldchain.ex.fa100.utils.chart.auxiliary;

import android.graphics.Canvas;
import android.graphics.DashPathEffect;

import com.fieldchain.ex.fa100.utils.chart.utils.data.KlineIndex;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.renderer.YAxisRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;

import static android.view.View.LAYER_TYPE_SOFTWARE;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/4
 */
public class MyYAxisRendererRight extends YAxisRenderer {
    private BarLineChartBase mChart;
    protected MyYAxisRight mYAxis;
    private int index=10;

    public MyYAxisRendererRight(ViewPortHandler viewPortHandler, MyYAxisRight yAxis, Transformer trans, BarLineChartBase chart) {
        super(viewPortHandler, yAxis, trans);
        mYAxis = yAxis;
        mChart = chart;
    }

    @Override
    protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
        int count = mYAxis.getYLabels().size();
        float offsetY=Utils.convertDpToPixel(5);
        float top=mViewPortHandler.contentTop();
        float bot=mViewPortHandler.contentBottom();
        float tags=(bot-top)/ (KlineIndex.klineIndex+2);
        top=top+tags;
        bot=bot-tags+offsetY;

        float dy=(bot-top)/(count-1);
        for (int i = 0; i < count; i++) {
            String text = mYAxis.getYLabels().valueAt(i);
            float yPos=bot-dy*i;
            int labelHight = Utils.calcTextHeight(mAxisLabelPaint, text);
            c.drawText(text, mViewPortHandler.contentRight()+Utils.convertDpToPixel(15), yPos, mAxisLabelPaint);
        }
    }


    /*网格线*/
    @Override
    public void renderGridLines(Canvas c) {

        int count = mYAxis.getYLabels().size();
        float offsetY=Utils.convertDpToPixel(5);
        float top=mViewPortHandler.contentTop();
        float bot=mViewPortHandler.contentBottom();
        float tags=(bot-top)/ (KlineIndex.klineIndex+2);
        top=top+tags;
        bot=bot-tags+offsetY;
        float dy=(bot-top)/(count-1);

        mGridPaint.setPathEffect ( new DashPathEffect( new float [ ] { 3, 5 }, 0 ) ) ;
        mChart.setLayerType(LAYER_TYPE_SOFTWARE, null);
        for (int i = 0; i < count; i++) {
            String label = mYAxis.getYLabels().valueAt(i);
            float yPos=bot-dy*i;
            int labelWidth = Utils.calcTextWidth(mAxisLabelPaint,m2(mYAxis.getMaxValue()) );
            int labelHight = Utils.calcTextHeight(mAxisLabelPaint, label);
            c.drawLine(labelWidth , yPos-labelHight/2,  mViewPortHandler.contentRight(), yPos-labelHight/2, mGridPaint);
        }
    }

    public String m2(float f) {
        DecimalFormat df = new DecimalFormat("#0.00");
        return df.format(f);
    }
}
